<template>
  <div class="pokedex">
    <div class="pokedex-header">
      <div class="left-controls">
        <el-button
          class="search-btn mobile-only"
          circle
          @click="showSearchDrawer = true"
        >
          <el-icon><Search /></el-icon>
        </el-button>
      </div>
      
      <h1>{{ t('pokedex.title') }}</h1>
    </div>

    <div class="pokedex-container">
      <!-- 搜索侧边栏 -->
      <!-- 移动端搜索抽屉 -->
      <el-drawer
        v-model="showSearchDrawer"
        direction="ltr"
        :with-header="false"
        size="280px"
        class="search-drawer mobile-only"
      >
        <div class="search-sidebar">
          <div class="search-box">
            <input 
              v-model="searchQuery" 
              type="text" 
              :placeholder="t('pokedex.search')"
              @input="handleSearch"
            >
            <div class="filter-section">
              <h3>{{ t('pokedex.typeFilter') }}</h3>
              <div class="type-filters">
                <button
                  v-for="type in pokemonTypes"
                  :key="type"
                  :class="['type-filter', type, { active: selectedTypes.includes(type) }]"
                  @click="toggleType(type)"
                >
                  {{ t(`pokedex.types.${type}`) }}
                </button>
              </div>
            </div>
          </div>
        </div>
      </el-drawer>

      <!-- PC端搜索侧边栏 -->
      <div class="search-sidebar desktop-only">
        <div class="search-box">
          <input 
            v-model="searchQuery" 
            type="text" 
            :placeholder="t('pokedex.search')"
            @input="handleSearch"
          >
          <div class="filter-section">
            <h3>{{ t('pokedex.typeFilter') }}</h3>
            <div class="type-filters">
              <button
                v-for="type in pokemonTypes"
                :key="type"
                :class="['type-filter', type, { active: selectedTypes.includes(type) }]"
                @click="toggleType(type)"
              >
                {{ t(`pokedex.types.${type}`) }}
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- 宝可梦列表 -->
      <div class="pokemon-grid" :class="cardSize">
        <div 
          v-for="pokemon in filteredPokemons" 
          :key="pokemon.id" 
          class="pokemon-card"
          :class="{ 'card-clicked': clickedCardId === pokemon.id }"
          @click="goToDetail(pokemon.id)"
        >
          <div class="pokemon-card-content">
            <img :src="pokemon.sprite" :alt="pokemon.name">
            <div class="pokemon-info">
              <span class="pokemon-number">#{{ pokemon.id.toString().padStart(3, '0') }}</span>
              <h3>{{ pokemon.name }}</h3>
              <div class="pokemon-types">
                <span v-for="type in pokemon.types" :key="type" :class="['type', type]">
                  {{ t(`pokedex.types.${type}`) }}
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, inject } from 'vue'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { Search } from '@element-plus/icons-vue'
import type { Pokemon } from '@/types/pokemon'

const router = useRouter()
const { t } = useI18n()
const pokemons = ref<Pokemon[]>([])
const searchQuery = ref('')
const selectedTypes = ref<string[]>([])

// 从 App.vue 获取卡片大小
const cardSize = inject<Ref<string>>('cardSize')!

// 所有可能的宝可梦属性
const pokemonTypes = [
  'normal', 'fire', 'water', 'electric', 'grass', 'ice',
  'fighting', 'poison', 'ground', 'flying', 'psychic', 'bug',
  'rock', 'ghost', 'dragon', 'dark', 'steel', 'fairy'
]

// 过滤宝可梦列表
const filteredPokemons = computed(() => {
  return pokemons.value.filter(pokemon => {
    // 名称搜索
    const nameMatch = pokemon.name.toLowerCase().includes(searchQuery.value.toLowerCase())
    
    // 属性过滤
    const typeMatch = selectedTypes.value.length === 0 || 
      pokemon.types.some(type => selectedTypes.value.includes(type))
    
    return nameMatch && typeMatch
  })
})

// 切换选中的属性
const toggleType = (type: string) => {
  const index = selectedTypes.value.indexOf(type)
  if (index === -1) {
    selectedTypes.value.push(type)
  } else {
    selectedTypes.value.splice(index, 1)
  }
}

// 处理搜索输入
const handleSearch = () => {
  // 可以在这里添加防抖逻辑
}

// 记录被点击的卡片ID
const clickedCardId = ref<number | null>(null)

const goToDetail = (id: number) => {
  clickedCardId.value = id
  // 添加短暂延迟让动画有时间执行
  setTimeout(() => {
    router.push(`/pokemon/${id}`)
  }, 300)
}

const fetchPokemons = async () => {
  try {
    const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=151')
    const data = await response.json()
    
    const pokemonDetails = await Promise.all(
      data.results.map(async (pokemon: any) => {
        const res = await fetch(pokemon.url)
        return res.json()
      })
    )
    
    pokemons.value = pokemonDetails.map((pokemon: any) => ({
      id: pokemon.id,
      name: pokemon.name,
      sprite: pokemon.sprites.front_default,
      types: pokemon.types.map((type: any) => type.type.name)
    }))
  } catch (error) {
    console.error('Error fetching pokemon:', error)
  }
}

onMounted(() => {
  fetchPokemons()
})

// 控制搜索抽屉的显示
const showSearchDrawer = ref(false)
</script>

<style scoped lang="scss">
.pokedex {
  min-height: 100vh;
  padding: 20px;
  background-color: inherit;
}

.pokedex-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 70px;
  padding: 0 20px;
  background-color: var(--el-bg-color);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);

  .left-controls {
    position: absolute;
    left: 20px;
    display: flex;
    gap: 10px;
  }

  h1 {
    margin: 0;
    text-align: center;
    @media (max-width: 768px) {
      display: none;
    }
  }
}

.pokedex-container {
  padding-top: 90px;
  display: flex;
  gap: 20px;
  max-width: 1400px;
  margin-left: auto;
  margin-right: auto;
  min-height: calc(100vh - 100px);
}

.search-sidebar {
  width: 280px;
  flex-shrink: 0;
  
  .search-box {
    position: sticky;
    top: 90px;
    background: var(--card-bg, white);
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);

    input {
      width: 100%;
      padding: 10px;
      border: 1px solid var(--border-color, #ddd);
      border-radius: 4px;
      margin-bottom: 20px;
      font-size: 16px;
      background: var(--input-bg, white);
      color: var(--text-color, inherit);

      &:focus {
        outline: none;
        border-color: #ee1515;
      }
    }
  }

  .filter-section {
    h3 {
      margin-bottom: 10px;
      color: #333;
    }
  }

  .type-filters {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;

    .type-filter {
      padding: 6px;
      border: none;
      border-radius: 4px;
      color: white;
      cursor: pointer;
      font-size: 0.9em;
      opacity: 0.6;
      transition: all 0.2s;

      &.active {
        opacity: 1;
        transform: scale(1.05);
      }

      // 使用现有的属性颜色
      &.grass { background-color: #78c850; }
      &.fire { background-color: #f08030; }
      &.water { background-color: #6890f0; }
      &.bug { background-color: #a8b820; }
      &.normal { background-color: #a8a878; }
      &.poison { background-color: #a040a0; }
      &.electric { background-color: #f8d030; }
      &.ground { background-color: #e0c068; }
      &.fairy { background-color: #ee99ac; }
      &.fighting { background-color: #c03028; }
      &.psychic { background-color: #f85888; }
      &.rock { background-color: #b8a038; }
      &.ghost { background-color: #705898; }
      &.ice { background-color: #98d8d8; }
      &.dragon { background-color: #7038f8; }
      &.dark { background-color: #705848; }
      &.steel { background-color: #b8b8d0; }
      &.flying { background-color: #a890f0; }
    }
  }
}

.pokemon-list {
  flex: 1;
  padding: 20px;
}

.pokemon-grid {
  display: grid;
  column-gap: 20px;
  row-gap: 20px;
  width: 100%;
  margin: 0 auto;
  padding: 20px;
  align-items: start;

  // 小尺寸卡片 (16个/行)
  &.small {
    grid-template-columns: repeat(16, 1fr);
    grid-auto-rows: min-content;

    @media (max-width: 2560px) { grid-template-columns: repeat(12, 1fr); }
    @media (max-width: 1920px) { grid-template-columns: repeat(10, 1fr); }
    @media (max-width: 1600px) { grid-template-columns: repeat(8, 1fr); }
    @media (max-width: 1366px) { grid-template-columns: repeat(6, 1fr); }
    @media (max-width: 1024px) { grid-template-columns: repeat(4, 1fr); }
    @media (max-width: 768px) { grid-template-columns: repeat(3, 1fr); }
    @media (max-width: 480px) { grid-template-columns: repeat(2, 1fr); }

    .pokemon-card {
      .pokemon-card-content {
        padding: 12px;
      }

      img {
        width: 80px;
        height: 80px;
      }

      h3 {
        font-size: 0.9em;
      }

      .type {
        font-size: 0.7em;
        padding: 2px 6px;
      }
    }
  }

  // 中等尺寸卡片 (12个/行)
  &.medium {
    grid-template-columns: repeat(12, 1fr);
    grid-auto-rows: min-content;

    @media (max-width: 2560px) { grid-template-columns: repeat(10, 1fr); }
    @media (max-width: 1920px) { grid-template-columns: repeat(8, 1fr); }
    @media (max-width: 1600px) { grid-template-columns: repeat(6, 1fr); }
    @media (max-width: 1366px) { grid-template-columns: repeat(4, 1fr); }
    @media (max-width: 1024px) { grid-template-columns: repeat(3, 1fr); }
    @media (max-width: 768px) { grid-template-columns: repeat(2, 1fr); }
    @media (max-width: 480px) { grid-template-columns: repeat(1, 1fr); }
  }

  // 大尺寸���片 (8个/行)
  &.large {
    grid-template-columns: repeat(8, 1fr);
    grid-auto-rows: min-content;

    @media (max-width: 2560px) { grid-template-columns: repeat(6, 1fr); }
    @media (max-width: 1920px) { grid-template-columns: repeat(5, 1fr); }
    @media (max-width: 1600px) { grid-template-columns: repeat(4, 1fr); }
    @media (max-width: 1366px) { grid-template-columns: repeat(3, 1fr); }
    @media (max-width: 1024px) { grid-template-columns: repeat(2, 1fr); }
    @media (max-width: 768px) { grid-template-columns: repeat(1, 1fr); }

    .pokemon-card {
      .pokemon-card-content {
        padding: 20px;
      }

      img {
        width: 160px;
        height: 160px;
      }

      h3 {
        font-size: 1.2em;
      }

      .type {
        font-size: 0.9em;
        padding: 4px 10px;
      }
    }
  }
}

.pokemon-card {
  aspect-ratio: 1;
  width: 100%;
  height: 100%;
  border: 1px solid var(--border-color, #ddd);
  border-radius: 8px;
  background: var(--card-bg, white);
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
  }

  .pokemon-card-content {
    height: 100%;
    padding: 16px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
  }
  
  img {
    width: 120px;
    height: 120px;
    object-fit: contain;
  }
  
  .pokemon-info {
    width: 100%;
    text-align: center;
    margin-top: 10px;
    
    .pokemon-number {
      color: #666;
      font-size: 0.9em;
    }
    
    h3 {
      margin: 5px 0;
      text-transform: capitalize;
      font-size: 1.1em;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  // 点击动画
  &.card-clicked {
    transform: scale(0.95);
    opacity: 0.8;
    transition: all 0.3s ease;
  }
}

.pokemon-types {
  display: flex;
  gap: 5px;
  justify-content: center;
  flex-wrap: wrap;
  
  .type {
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 0.8em;
    color: white;
    
    &.grass { background-color: #78c850; }
    &.fire { background-color: #f08030; }
    &.water { background-color: #6890f0; }
    &.bug { background-color: #a8b820; }
    &.normal { background-color: #a8a878; }
    &.poison { background-color: #a040a0; }
    &.electric { background-color: #f8d030; }
    &.ground { background-color: #e0c068; }
    &.fairy { background-color: #ee99ac; }
    &.fighting { background-color: #c03028; }
    &.psychic { background-color: #f85888; }
    &.rock { background-color: #b8a038; }
    &.ghost { background-color: #705898; }
    &.ice { background-color: #98d8d8; }
    &.dragon { background-color: #7038f8; }
  }
}

/* 添加深色主题支持 */
:global(.dark) {
  --card-bg: #333;
  --border-color: #444;
  --input-bg: #222;
  --text-color: white;

  .pokemon-card {
    background: var(--card-bg);
    border-color: var(--border-color);
  }

  .search-box {
    background: var(--card-bg);
  }

  input {
    background: var(--input-bg);
    border-color: var(--border-color);
    color: var(--text-color);
  }

  .pokemon-number {
    color: #aaa;
  }
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  padding: 0 20px;
  flex-wrap: wrap;
  gap: 20px;

  h1 {
    margin: 0;
    white-space: nowrap;
  }

  .controls {
    display: flex;
    gap: 16px;
    align-items: center;
    flex-wrap: wrap;
  }

  .menu-button {
    display: none; // 默认隐藏菜单按钮
  }
}

// 移动端适配
@media (max-width: 768px) {
  .header {
    .menu-button {
      display: block; // 显示菜单按钮
    }
  }

  .desktop-only {
    display: none; // 隐藏桌面端侧栏
  }

  .pokedex-container {
    padding: 0;
  }

  .pokemon-list {
    padding: 10px;
  }
}

// 移动端抽屉样式
.mobile-sidebar {
  :deep(.el-drawer__body) {
    padding: 0;
  }

  .search-sidebar {
    width: 100%;
    height: 100%;
    overflow-y: auto;
  }

  .search-box {
    border-radius: 0;
    height: 100%;
  }
}

/* 搜索抽屉样式 */
.search-drawer {
  :deep(.el-drawer__body) {
    padding: 0;
    overflow: hidden;
    display: flex;
    justify-content: center;
  }

  .search-sidebar {
    width: 80%;
    max-width: 280px;
    height: 100%;
    padding: 20px;
    overflow-y: auto;
    overflow-x: hidden;

    .search-box {
      width: 100%;
      border-radius: 8px;
      background: var(--el-bg-color);
      box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    }

    .type-filters {
      grid-template-columns: repeat(2, 1fr);
      gap: 8px;
      width: 100%;
    }
  }
}

// 移动端和桌面端显示控制
.mobile-only {
  display: none;
}

@media (max-width: 768px) {
  .mobile-only {
    display: block;
  }

  .desktop-only {
    display: none;
  }

  .pokedex-container {
    padding-left: 10px;
    padding-right: 10px;
  }

  .pokemon-grid {
    padding: 10px;
  }

  .search-drawer {
    .search-sidebar {
      padding: 10px;

      .search-box {
        padding: 15px;
      }
    }
  }
}
</style> 